﻿using System.Data.Linq;
using System.Linq;
using UniMana.Data;
using System;
namespace UniMana.Data
{
    public static class Personnel_Personnel_SubjectRegistData_Chi
    {
        /// <summary>
        /// lấy danh sách giảng viên thuộc ngày tháng
        /// </summary>
        /// <param name="personnel_SubjectRegist"></param>
        /// <param name="session_Subject_Class_Room"></param>
        /// <param name="subject_ClassKey"></param>
        /// <param name="startDay"></param>
        /// <param name="finishDay"></param>
        /// <returns></returns>
        public static IQueryable GetBy_subject_start_finishday(
                this Table<Personnel_Personnel_SubjectRegist> personnel_SubjectRegist,
                Table<Personnel_Personnel> personnel,
                int subjectKey,
                DateTime startDay,
                DateTime finishDay)
        {
            var Query = from p_sj_re in personnel_SubjectRegist
                        join p in personnel on p_sj_re.Personnel_PersonnelKey equals p.Personnel_PersonnelKey
                        where
                          p_sj_re.Training_SubjectKey == subjectKey &&
                          p_sj_re.StartTime <= startDay &&
                          p_sj_re.EndTime >= finishDay
                        select p;
            return Query;
        }


        #region [An]
        /// <summary>
        /// Lấy danh sách môn giảng viên đã đăng ký
        /// </summary>
        /// <param name="nPersonSubjectRegist"></param>
        /// <param name="m_DateCurrent"></param>
        /// <returns></returns>

        public static IQueryable GetSubjectRegist(this Table<Personnel_Personnel_SubjectRegist> nPersonnel_Personnel_SubjectRegist,
            Table<Training_Subject> nTraining_Subject,
            Table<Training_Subject_Open> nTraining_Subject_Open,
            string m_strPersonnel_PersonnelKey,
            DateTime m_dDateCurrent)
        {
            var values = from r in nPersonnel_Personnel_SubjectRegist
                         join s in nTraining_Subject on r.Training_SubjectKey equals s.Training_SubjectKey
                         //where r.StartTime >= m_dDateCurrent
                         //where r.EndTime >= m_dDateCurrent
                         where r.Personnel_PersonnelKey.ToString() == m_strPersonnel_PersonnelKey
                         select new
                         {
                             r.Personnel_Personnel_SubjectRegistKey,
                             s.Catalogue_DepartmentKey,
                             r.Training_SubjectKey,
                             s.ID,
                             s.Name,
                             s.TotalCredits
                         };
            var open = from t in nTraining_Subject_Open
                       where
                         t.Closed == false
                       select new
                       {
                           t.Training_SubjectKey
                       };

            var final = from v in values
                        join o in open on v.Training_SubjectKey equals o.Training_SubjectKey
                        select new
                        {
                            v.Personnel_Personnel_SubjectRegistKey,
                            v.Catalogue_DepartmentKey,
                            v.Training_SubjectKey,
                            v.ID,
                            v.Name,
                            v.TotalCredits
                        };

            return final.Distinct();
        }
        #endregion
    }
}